<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>Python 数据分析速查表（中文版）</title>
  <style>
    body {
      font-family: "Microsoft YaHei", "SimSun", sans-serif;
      line-height: 1.6;
      margin: 40px auto;
      max-width: 900px;
      padding: 20px;
      background-color: #fff;
      color: #333;
    }
    h1, h2, h3 {
      color: #2c3e50;
      margin-top: 1.5em;
    }
    h1 {
      text-align: center;
      border-bottom: 2px solid #3498db;
      padding-bottom: 10px;
      margin-bottom: 30px;
    }
    pre {
      background-color: #f8f9fa;
      padding: 12px;
      border-left: 4px solid #3498db;
      overflow-x: auto;
      font-family: Consolas, Monaco, "Courier New", monospace;
      font-size: 14px;
      margin: 15px 0;
      border-radius: 4px;
    }
    code {
      font-family: Consolas, Monaco, "Courier New", monospace;
      background-color: #f1f1f1;
      padding: 2px 4px;
      border-radius: 3px;
    }
    .footer {
      text-align: center;
      margin-top: 40px;
      color: #7f8c8d;
      font-size: 0.9em;
    }
    ul, ol {
      margin-bottom: 15px;
    }
  </style>
</head>
<body>

<h1>Python 数据分析速查表（中文版）</h1>
<p style="text-align: center; color: #7f8c8d;">版权所有 © IBM 公司 2023。保留所有权利</p>

<h2>数据加载</h2>
<ul>
  <li><strong>读取 CSV 数据集</strong>
    <pre># 不带表头加载
df = pd.read_csv(&lt;CSV路径&gt;, header=None)

# 使用第一行作为表头加载
df = pd.read_csv(&lt;CSV路径&gt;, header=0)</pre>
  </li>

  <li><strong>打印前几行数据</strong>
    <pre># n=显示的行数；默认为5
df.head(n)</pre>
  </li>

  <li><strong>打印后几行数据</strong>
    <pre># n=显示的行数；默认为5
df.tail(n)</pre>
  </li>

  <li><strong>指定列名</strong>
    <pre>df.columns = headers</pre>
  </li>

  <li><strong>将 “?” 替换为 NaN</strong>
    <pre>df = df.replace("?", np.nan)</pre>
  </li>

  <li><strong>查看各列数据类型</strong>
    <pre>df.dtypes</pre>
  </li>

  <li><strong>获取统计摘要</strong>
    <pre># 默认仅数值型列
df.describe()

# 包含所有列（包括非数值型）
df.describe(include="all")</pre>
  </li>

  <li><strong>查看数据集概要信息</strong>
    <pre>df.info()</pre>
  </li>

  <li><strong>将 DataFrame 保存为 CSV</strong>
    <pre>df.to_csv(&lt;输出CSV路径&gt;)</pre>
  </li>
</ul>

<h2>数据清洗（Data Wrangling）</h2>
<ul>
  <li><strong>用众数填充缺失值</strong>
    <pre>MostFrequentEntry = df['属性名'].value_counts().idxmax()
df['属性名'].replace(np.nan, MostFrequentEntry, inplace=True)</pre>
  </li>

  <li><strong>用均值填充缺失值</strong>
    <pre>AverageValue = df['属性'].astype(&lt;数据类型&gt;).mean(axis=0)
df['属性'].replace(np.nan, AverageValue, inplace=True)</pre>
  </li>

  <li><strong>修正数据类型</strong>
    <pre>df[['属性1', '属性2', ...]] = df[['属性1', '属性2', ...]].astype('数据类型')
# 数据类型可以是 int、float、str 等</pre>
  </li>

  <li><strong>数据归一化</strong>
    <pre>df['属性名'] = df['属性名'] / df['属性名'].max()</pre>
  </li>

  <li><strong>分箱（Binning）</strong>
    <pre>bins = np.linspace(min(df['属性名']), max(df['属性名']), n)  # n 为所需箱数
GroupNames = ['组1', '组2', '组3', ...]
df['分箱后属性名'] = pd.cut(df['属性名'], bins, labels=GroupNames, include_lowest=True)</pre>
  </li>

  <li><strong>重命名列</strong>
    <pre>df.rename(columns={'旧名称': '新名称'}, inplace=True)</pre>
  </li>

  <li><strong>创建虚拟变量（One-Hot 编码）</strong>
    <pre>dummy_variable = pd.get_dummies(df['属性名'])
df = pd.concat([df, dummy_variable], axis=1)</pre>
  </li>
</ul>

<h2>探索性数据分析（EDA）</h2>
<ul>
  <li><strong>整个 DataFrame 的相关性矩阵</strong>
    <pre>df.corr()</pre>
  </li>

  <li><strong>特定属性之间的相关性</strong>
    <pre>df[['属性1', '属性2', ...]].corr()</pre>
  </li>

  <li><strong>散点图</strong>
    <pre>from matplotlib import pyplot as plt
plt.scatter(df['属性1'], df['属性2'])</pre>
  </li>

  <li><strong>回归图</strong>
    <pre>import seaborn as sns
sns.regplot(x='属性1', y='属性2', data=df)</pre>
  </li>

  <li><strong>箱线图</strong>
    <pre>sns.boxplot(x='属性1', y='属性2', data=df)</pre>
  </li>

  <li><strong>按属性分组</strong>
    <pre>df_group = df[['属性1', '属性2', ...]]

# 按单个属性分组并计算均值
df_group = df_group.groupby(['属性1'], as_index=False).mean()

# 按多个属性分组并计算均值
df_group = df_group.groupby(['属性1', '属性2'], as_index=False).mean()</pre>
  </li>

  <li><strong>创建透视表</strong>
    <pre>grouped_pivot = df_group.pivot(index='属性1', columns='属性2')</pre>
  </li>

  <li><strong>伪彩色图（热力图）</strong>
    <pre>plt.pcolor(grouped_pivot, cmap='RdBu')</pre>
  </li>

  <li><strong>皮尔逊相关系数与 p 值</strong>
    <pre>from scipy import stats
pearson_coef, p_value = stats.pearsonr(df['属性1'], df['属性2'])</pre>
  </li>
</ul>

<h2>模型开发</h2>
<ul>
  <li><strong>线性回归</strong>
    <pre>from sklearn.linear_model import LinearRegression
lr = LinearRegression()

X = df[['属性1', '属性2', ...]]
Y = df['目标属性']
lr.fit(X, Y)

# 预测
Y_hat = lr.predict(X)

# 获取系数和截距
coeff = lr.coef_
intercept = lr.intercept_</pre>
  </li>

  <li><strong>残差图</strong>
    <pre>sns.residplot(x=df['属性1'], y=df['属性2'])</pre>
  </li>

  <li><strong>分布图</strong>
    <pre>sns.distplot(df['属性名'], hist=False)
# 可添加 color、label 等参数</pre>
  </li>

  <li><strong>多项式回归</strong>
    <pre>f = np.polyfit(x, y, n)   # n 为多项式阶数
p = np.poly1d(f)            # p 为拟合的多项式模型
Y_hat = p(x)                # 预测值</pre>
  </li>

  <li><strong>多元多项式回归（使用管道）</strong>
    <pre>from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.pipeline import Pipeline

Z = df[['属性1', '属性2', ...]]
pr = PolynomialFeatures(degree=n)
Z_pr = pr.fit_transform(Z)

Input = [
    ('scale', StandardScaler()),
    ('polynomial', PolynomialFeatures(include_bias=False)),
    ('model', LinearRegression())
]
pipe = Pipeline(Input)
Z = Z.astype(float)
pipe.fit(Z, y)
ypipe = pipe.predict(Z)</pre>
  </li>

  <li><strong>R² 决定系数</strong>
    <pre># 线性回归
R2_score = lr.score(X, Y)

# 多项式回归
from sklearn.metrics import r2_score
R2_score = r2_score(y, p(x))</pre>
  </li>

  <li><strong>均方误差（MSE）</strong>
    <pre>from sklearn.metrics import mean_squared_error
mse = mean_squared_error(Y, Y_hat)</pre>
  </li>
</ul>

<h2>模型评估与优化</h2>
<ul>
  <li><strong>划分训练集与测试集</strong>
    <pre>from sklearn.model_selection import train_test_split

y_data = df['目标属性']
x_data = df.drop('目标属性', axis=1)
x_train, x_test, y_train, y_test = train_test_split(
    x_data, y_data, test_size=0.10, random_state=1
)</pre>
  </li>

  <li><strong>交叉验证得分</strong>
    <pre>from sklearn.model_selection import cross_val_score

lre = LinearRegression()
Rcross = cross_val_score(lre, x_data[['属性1']], y_data, cv=n)  # n 为折数
Mean = Rcross.mean()
Std_dev = Rcross.std()</pre>
  </li>

  <li><strong>交叉验证预测</strong>
    <pre>yhat = cross_val_predict(lre, x_data[['属性1']], y_data, cv=4)</pre>
  </li>

  <li><strong>岭回归（Ridge Regression）</strong>
    <pre>from sklearn.linear_model import Ridge

pr = PolynomialFeatures(degree=2)
x_train_pr = pr.fit_transform(x_train[['属性1', '属性2', ...]])
x_test_pr = pr.transform(x_test[['属性1', '属性2', ...]])  # 注意：用 transform

RidgeModel = Ridge(alpha=1)
RidgeModel.fit(x_train_pr, y_train)
yhat = RidgeModel.predict(x_test_pr)</pre>
  </li>

  <li><strong>网格搜索（调参）</strong>
    <pre>from sklearn.model_selection import GridSearchCV

parameters = [{'alpha': [0.001, 0.1, 1, 10, 100, 1000, 10000]}]
RR = Ridge()
Grid1 = GridSearchCV(RR, parameters, cv=4)
Grid1.fit(x_data[['属性1', '属性2', ...]], y_data)

BestRR = Grid1.best_estimator_
BestRR.score(x_test[['属性1', '属性2', ...]], y_test)</pre>
  </li>
</ul>

<div class="footer">
  <p>Python 数据分析速查表 · 中文翻译版 · 生成于 2025年11月11日</p>
</div>

</body>
</html>